package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.formula.service.FunctionService;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.RuleOperationEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.help.ComplexFormatHelp;
import com.xbongbong.paas.pojo.OperationsPojo;
import com.xbongbong.paas.service.BusinessRuleAdvancedService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PaasStringUtil;
import com.xbongbong.paas.util.BusinessRuleExplainUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.condition.ConditionFieldHandler;
import com.xbongbong.parent.condition.ConditionFieldHandlerFactory;
import com.xbongbong.parent.util.ConditionUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleExplainPojo;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.businessrule.pojo.EqualsResultPojo;
import com.xbongbong.pro.businessrule.pojo.dto.OperationDTO;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.domain.entity.UpdateDataValueEntity;
import com.xbongbong.pro.domain.entity.UpdateSubDataEntity;
import com.xbongbong.pro.enums.businessrule.enums.RuleValueTypeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.RuleTargetPojo;
import com.xbongbong.util.DataTypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2020/10/09 10:55
 */
@Service("businessRuleAdvancedService")
public class BusinessRuleAdvancedServiceImpl implements BusinessRuleAdvancedService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessRuleAdvancedServiceImpl.class);

    @Resource
    private ComplexFormatHelp complexFormatHelp;
    @Resource
    private FunctionService functionService;
    @Resource
    private ConditionFieldHandlerFactory conditionFieldHandlerFactory;

    @Override
    public List<UpdateDataEntity> getUpdateDataByFormula(List<PaasFormDataEntityExt> formDataList, List<? extends PaasFormSubDataEntity> subFormDataList, OperationDTO operationDTO) throws XbbException {
        List<UpdateDataEntity> updateDataList = new ArrayList<>();
        if (CollectionsUtil.isEmpty(formDataList)) {
            return updateDataList;
        }
        try {
            // 获取子表单的字段解释
            BusinessRuleExplainPojo businessRuleExplainPojo = BusinessRuleExplainUtil.getSubExplainMap(operationDTO);

            if (CollectionsUtil.isNotEmpty(operationDTO.getOperations())) {
                List<OperationsPojo> subOperationList = new ArrayList<>();
                List<OperationsPojo> mainOperationList = new ArrayList<>();
                operationDTO.getOperations().forEach(item->{
                    String targetAttr = item.getAttr();
                    List<String> targetAttrList = ExplainUtil.getAttrByPoint(targetAttr);
                    if (targetAttrList.size() == 2) {
                        subOperationList.add(item);
                    } else {
                        mainOperationList.add(item);
                    }
                });
                // 获取主字段的更新数据
                String corpid = operationDTO.getCorpid();
                Integer businessType = operationDTO.getBusinessType();
                BusinessRuleInfoPojo businessRuleInfoPojo = new BusinessRuleInfoPojo(corpid, operationDTO.getThisExplain(), operationDTO.getTargetExplain(), operationDTO.getTarget(), operationDTO.getFormId(),
                        businessType, operationDTO.getSaasMark(), operationDTO.getOperationType(), operationDTO.getFormDataMap());
                // 获取主表更新的数据
                updateDataList = getMainUpdateDataList(formDataList, subFormDataList, mainOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo);
                if (CollectionsUtil.isNotEmpty(subOperationList)) {
                    // 获取子表单字段需要更新数据
                    List<UpdateSubDataEntity> updateSubDataList = getSubUpdateDataList(formDataList, subFormDataList, subOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo);
                    // 将子表单放入主表中，主要是为了拼装数据
                    String parentAttr = ExplainUtil.getAttrByPoint(subOperationList.get(0).getAttr()).get(0);
                    DataTypeUtil.setSubDataList(updateDataList, updateSubDataList, parentAttr, corpid);

                }
            }

        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("BusinessRuleAdvancedServiceImpl.getUpdateDataByFormula 业务规则高级版执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return updateDataList;
    }


    /**
     * 获取子表单更新的数据
     * @param formDataList 目标表单主表数据
     * @param subFormDataList 目标表单子表单数据
     * @param subOperationList 子表单字段操作方式
     * @param operationDTO
     * @param businessRuleExplainPojo
     * @return java.util.List<com.xbongbong.pro.domain.entity.UpdateSubDataEntity>
     * @author 吴峰
     * @date 2020/10/10 15:01
     * @throws XbbException
     */
    private List<UpdateSubDataEntity> getSubUpdateDataList(List<PaasFormDataEntityExt> formDataList, List<? extends PaasFormSubDataEntity> subFormDataList, List<OperationsPojo> subOperationList, OperationDTO operationDTO,
                                                         BusinessRuleExplainPojo businessRuleExplainPojo, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException{
        List<UpdateSubDataEntity> updateDataList = new ArrayList<>();
        try {
            String corpid = operationDTO.getCorpid();
            // 获取主表的数据集合
            Map<Long, PaasFormDataEntityExt> formDataMap = new HashMap<>(formDataList.size());
            formDataList.forEach(item->formDataMap.put(item.getDataId(), item));
            // 子表单和主表单的对应
            Map<Long, List<PaasFormSubDataEntity>> subFormDataMap = new HashMap<>();
            subFormDataList.forEach(item -> {
                subFormDataMap.computeIfAbsent(item.getDataId(), v -> new ArrayList<>()).add(item);
            });

            PaasFormDataEntity thisFormData = operationDTO.getPaasFormDataEntity();
            // 条件为：目标表单表单主字段等于当前子表单内字段
            boolean isMainAndSubForm = businessRuleExplainPojo.getIsMainAndSubForm();
            boolean isSubFormAndSubForm = businessRuleExplainPojo.getIsSubFormAndSubForm();
            // 目标表单
            Map<String, FieldAttrEntity> targetExplainMap = operationDTO.getTargetExplain();
            Map<String, FieldAttrEntity> targetSubExplainMap = businessRuleExplainPojo.getTargetSubExplainMap();
            // 本表单
            Map<String, FieldAttrEntity> thisExplain = operationDTO.getThisExplain();
            Map<String, FieldAttrEntity> thisSubExplainMap = businessRuleExplainPojo.getThisSubExplainMap();

            RuleTargetPojo ruleTargetPojo = operationDTO.getTarget();

            Iterator<Map.Entry<Long, List<PaasFormSubDataEntity>>> iterator = subFormDataMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Long, List<PaasFormSubDataEntity>> targetFormData = iterator.next();
                Long parentId = targetFormData.getKey();
                List<PaasFormSubDataEntity> value = targetFormData.getValue();
                PaasFormDataEntityExt parentFormData = formDataMap.get(parentId);

                JSONObject thisData = thisFormData.getData();
                JSONObject thisSubData = thisFormData.getSubFormData() == null ? new JSONObject() : thisFormData.getSubFormData();


                if (isSubFormAndSubForm) {
                    JSONArray thisSubDataArray = thisSubData.getJSONArray(businessRuleExplainPojo.getThisKey()) == null ? new JSONArray() : thisSubData.getJSONArray(businessRuleExplainPojo.getThisKey());
                    for (int i = 0; i < thisSubDataArray.size(); i++) {
                        JSONObject subDataJson = thisSubDataArray.getJSONObject(i);

                        PaasFormSubDataEntity equalFormData = null;
                        Iterator<PaasFormSubDataEntity> iter = value.iterator();
                        while (iter.hasNext()) {
                            PaasFormSubDataEntity paasFormSubDataEntity = iter.next();
                            List<FilterConditionsPoJo> subConditionsList = businessRuleExplainPojo.getSubConditionsList();

                            JSONObject data = paasFormSubDataEntity.getData();

                            boolean isEqual = false;
                            for (FilterConditionsPoJo filterConditionsPoJo : subConditionsList) {
                                if (Objects.isNull(subDataJson)) {
                                    continue;
                                }
                                String targetKey = filterConditionsPoJo.getAttr();
                                if (Objects.equals(RuleValueTypeEnum.DYNAMIC_VALUE.getType(), filterConditionsPoJo.getValueType())) {
                                    List<String> thisAttrList = ExplainUtil.getAttrByPoint(filterConditionsPoJo.getValueAttr());
                                    FieldAttrEntity targetField;
                                    if (targetKey.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                                        targetField = targetSubExplainMap.get(ExplainUtil.getAttrByPoint(targetKey).get(1));

                                    } else {
                                        targetField = targetExplainMap.get(targetKey);
                                    }

                                    if (thisAttrList.size() == 2) {
                                        String thisSubKey = thisAttrList.get(1);
                                        FieldAttrEntity fieldAttrEntity = thisSubExplainMap.get(thisSubKey);

                                        // 获取目标表单数据
                                        ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(targetField.getFieldType());
                                        Object targetValue = conditionFieldHandler.handlerThisFormField(parentFormData, data, targetField, fieldAttrEntity, businessRuleInfoPojo);

                                        // 获取本表单数据
                                        ConditionFieldHandler thisConditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(fieldAttrEntity.getFieldType());
                                        Object object = thisConditionFieldHandler.handlerThisFormField(thisFormData, subDataJson, fieldAttrEntity, targetField, businessRuleInfoPojo);
                                        isEqual = ConditionUtil.equals(targetValue, object, filterConditionsPoJo);
                                    } else {
                                        FieldAttrEntity fieldAttrEntity = thisExplain.get(filterConditionsPoJo.getValueAttr());
                                        // 获取目标表单数据
                                        ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(targetField.getFieldType());
                                        Object targetValue = conditionFieldHandler.handlerThisFormField(parentFormData, data, targetField, fieldAttrEntity, businessRuleInfoPojo);

                                        // 获取本表单数据
                                        ConditionFieldHandler thisConditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(fieldAttrEntity.getFieldType());
                                        Object object = thisConditionFieldHandler.handlerThisFormField(thisFormData, thisData, fieldAttrEntity, targetField, businessRuleInfoPojo);
                                        isEqual = ConditionUtil.equals(targetValue, object, filterConditionsPoJo);
                                    }

                                } else {
                                    FieldAttrEntity targetField;
                                    if (targetKey.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                                        targetField = targetSubExplainMap.get(ExplainUtil.getAttrByPoint(targetKey).get(1));
                                    } else {
                                        targetField = targetExplainMap.get(targetKey);
                                    }
                                    // 获取目标表单数据
                                    ConditionFieldHandler conditionFieldHandler = conditionFieldHandlerFactory.getHandlerTargetFormByFieldType(targetField.getFieldType());
                                    Object targetValue = conditionFieldHandler.handlerThisFormField(parentFormData, data, targetField, targetField, businessRuleInfoPojo);

                                    // 获取当前表单的值
                                    ConditionFieldHandler thisFieldCondition = conditionFieldHandlerFactory.getHandlerFixedValueByFieldType(targetField.getFieldType());
                                    Object object = thisFieldCondition.handlerFixedValue(targetField, businessRuleInfoPojo, filterConditionsPoJo);
                                    if (ExplainUtil.isCombo(targetField.getFieldType())) {
                                        businessRuleInfoPojo.setObject(object);
                                        Object result = thisFieldCondition.handlerThisFormField(thisFormData, thisData, targetField, targetField, businessRuleInfoPojo);
                                        isEqual = ConditionUtil.equals(targetValue, result, filterConditionsPoJo);
                                    } else {
                                        isEqual = ConditionUtil.equals(targetValue, object, filterConditionsPoJo);
                                    }
                                }

                                if (!isEqual) {
                                    break;
                                }
                            }

                            if (isEqual) {
                                equalFormData = paasFormSubDataEntity;
                            }

                            if (!Objects.isNull(equalFormData)) {
                                UpdateSubDataEntity updateSubDataEntity = new UpdateSubDataEntity();
                                updateSubDataEntity.setSubDataId(equalFormData.getId());
                                updateSubDataEntity.setParentId(parentId);
                                updateSubDataEntity.setFormId(equalFormData.getFormId());
                                List<UpdateDataValueEntity> updateDataValueList = new ArrayList<>();
                                extracted(subOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo, corpid, targetExplainMap, targetSubExplainMap, ruleTargetPojo, equalFormData.getData(), subDataJson, parentFormData, updateDataValueList);
                                if (CollectionsUtil.isNotEmpty(updateDataValueList)) {
                                    updateSubDataEntity.setSubDataList(updateDataValueList);
                                    updateDataList.add(updateSubDataEntity);
                                }
                                break;
                            } else {
                                if (Objects.equals(operationDTO.getOperationType(), RuleOperationEnum.UPDATE_AND_INSERT.getType()) && !iter.hasNext()) {
                                    UpdateSubDataEntity subDataEntity = new UpdateSubDataEntity();
                                    subDataEntity.setSubDataId(null);
                                    subDataEntity.setParentId(parentId);
                                    subDataEntity.setFormId(parentFormData.getFormId());
                                    List<UpdateDataValueEntity> insertDataValueList = new ArrayList<>();
                                    extracted(subOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo, corpid, targetExplainMap, targetSubExplainMap, ruleTargetPojo, new JSONObject(), subDataJson, parentFormData, insertDataValueList);
                                    subDataEntity.setSubDataList(insertDataValueList);
                                    updateDataList.add(subDataEntity);
                                }
                            }
                        }


                    }

                } else if (isMainAndSubForm || businessRuleExplainPojo.getIsSubFormAndMain()) {
                    for (PaasFormSubDataEntity paasFormSubDataEntity : value) {
                        UpdateSubDataEntity updateSubDataEntity = new UpdateSubDataEntity();
                        updateSubDataEntity.setSubDataId(paasFormSubDataEntity.getId());
                        updateSubDataEntity.setParentId(parentId);
                        updateSubDataEntity.setFormId(parentFormData.getFormId());
                        List<UpdateDataValueEntity> updateDataValueList= new ArrayList<>();
                        extracted(subOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo, corpid, targetExplainMap, targetSubExplainMap, ruleTargetPojo, paasFormSubDataEntity.getData(), operationDTO.getPaasFormDataEntity().getData(), parentFormData, updateDataValueList);
                        if (CollectionsUtil.isNotEmpty(updateDataValueList)) {
                            updateSubDataEntity.setSubDataList(updateDataValueList);
                            updateDataList.add(updateSubDataEntity);
                        }
                    }
                }

            }

//
//            for (PaasFormSubDataEntity targetFormData: subFormDataList) {
//                JSONObject targetData = targetFormData.getData();
//                UpdateSubDataEntity updateSubDataEntity = new UpdateSubDataEntity();
//                updateSubDataEntity.setSubDataId(targetFormData.getId());
//                updateSubDataEntity.setParentId(targetFormData.getDataId());
//                updateSubDataEntity.setFormId(targetFormData.getFormId());
//                // 获取主表数据
//                PaasFormDataEntityExt parentFormData = formDataMap.get(targetFormData.getDataId());
//                if (isSubFormAndSubForm || isMainAndSubForm){
//                    // 当前表单子表单字段赋值目标表单主字段
//                    EqualsResultPojo updateResultPojo = complexFormatHelp.equalsByCondition(parentFormData, targetFormData.getData(), operationDTO, null);
//                    List<JSONObject> resultList = updateResultPojo.getResultList();
//                    if (CollectionsUtil.isNotEmpty(resultList) && updateResultPojo.getUpdate()) {
//                        List<UpdateDataValueEntity> updateDataValueList= new ArrayList<>();
//                        JSONObject resultJson = resultList.get(resultList.size()-1);
//                        extracted(subOperationList, operationDTO, businessRuleExplainPojo, businessRuleInfoPojo, corpid, targetExplainMap, targetSubExplainMap, ruleTargetPojo, targetData, resultJson, parentFormData, updateDataValueList);
//                        if (CollectionsUtil.isNotEmpty(updateDataValueList)) {
//                            updateSubDataEntity.setSubDataList(updateDataValueList);
//                            updateDataList.add(updateSubDataEntity);
//                        }
//                    } else {
//                        // 没有符合条件的数据，不用更新数据
//                        continue;
//                    }
//                }
//
//            }
        } catch (XbbException e) {
            throw e;
        }catch (Exception e) {
            LOGGER.error("BusinessRuleAdvancedServiceImpl.getSubUpdateDataList 获取子表单更新数据执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return updateDataList;
    }

    private void extracted(List<OperationsPojo> subOperationList, OperationDTO operationDTO, BusinessRuleExplainPojo businessRuleExplainPojo, BusinessRuleInfoPojo businessRuleInfoPojo, String corpid, Map<String, FieldAttrEntity> targetExplainMap, Map<String, FieldAttrEntity> targetSubExplainMap, RuleTargetPojo ruleTargetPojo, JSONObject targetData, JSONObject resultJson, PaasFormDataEntityExt parentFormData, List<UpdateDataValueEntity> updateDataValueList) throws XbbException {
        Iterator<OperationsPojo> iterator = subOperationList.iterator();
        while (iterator.hasNext()) {
            UpdateDataValueEntity updateDataValueEntity = new UpdateDataValueEntity();
            OperationsPojo operationsPojo = iterator.next();
            String targetAttr = operationsPojo.getAttr();
            List<String> targetAttrList = ExplainUtil.getAttrByPoint(targetAttr);
            String targetSubAttr = "";
            if (targetAttrList.size() == 2) {
                targetAttr = targetAttrList.get(0);
                targetSubAttr = targetAttrList.get(1);
            }
            FieldAttrEntity targetField = targetExplainMap.get(targetAttr);
            if (Objects.isNull(targetField)) {
                iterator.remove();
                continue;
            }
            FieldAttrEntity targetSubField = targetSubExplainMap.get(targetSubAttr);
            if (Objects.isNull(targetSubField)) {
                iterator.remove();
                continue;
            }
            updateDataValueEntity.setKey(targetSubAttr);
            updateDataValueEntity.setEsKey(targetSubAttr);
            if (ExplainUtil.isSubForm(targetField.getFieldType())) {
                // 赋值给主字段
                Map<String, Object> data = targetData.getInnerMap();
                EqualsResultPojo equalsResultPojo = complexFormatHelp.newComplexFormat(operationDTO, operationsPojo, parentFormData, parentFormData.getData(),
                        businessRuleExplainPojo, businessRuleInfoPojo, resultJson, targetData);
                if (equalsResultPojo.getUpdate()) {
                    String formula = equalsResultPojo.getFormula();
                    Object value = functionService.expressRun(formula, data);
                    value = PaasStringUtil.replaceQuotes(value);

                    value = complexFormatHelp.getTargetDataByFieldType(targetExplainMap, value, operationsPojo, targetSubField, ruleTargetPojo, corpid, businessRuleInfoPojo);
                    value = DataTypeUtil.setDataType(updateDataValueEntity, value, targetField.getFieldType());
                    updateDataValueEntity.setValue(value);
                    updateDataValueList.add(updateDataValueEntity);
                }
            }
        }
    }

    /**
     * 获取主表更新的数据
     * @param formDataList
     * @param subFormDataList
     * @param mainOperationList
     * @param operationDTO
     * @param businessRuleExplainPojo
     * @return java.util.List<com.xbongbong.pro.domain.entity.UpdateDataEntity>
     * @author 吴峰
     * @date 2020/10/10 14:54
     * @throws XbbException
     */
    private List<UpdateDataEntity> getMainUpdateDataList(List<PaasFormDataEntityExt> formDataList, List<? extends PaasFormSubDataEntity> subFormDataList, List<OperationsPojo> mainOperationList, OperationDTO operationDTO,
                                                         BusinessRuleExplainPojo businessRuleExplainPojo, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException{
        if (CollectionsUtil.isEmpty(mainOperationList)) {
            return new ArrayList<>();
        }
        List<UpdateDataEntity> updateDataList = new ArrayList<>();
        String corpid = operationDTO.getCorpid();
        PaasFormDataEntity thisFormData = operationDTO.getPaasFormDataEntity();
        // 拼装对象
        long now = DateTimeUtil.getInt() + 1;

        boolean isSubFormAndSubForm = businessRuleExplainPojo.getIsSubFormAndSubForm();

        // 条件为：目标表单表单主字段等于当前子表单内字段
        boolean isMainAndSubForm = businessRuleExplainPojo.getIsMainAndSubForm();
        Map<String, FieldAttrEntity> targetExplainMap = operationDTO.getTargetExplain();
        RuleTargetPojo ruleTargetPojo = operationDTO.getTarget();
        for (PaasFormDataEntityExt targetFormData: formDataList) {
            JSONObject targetData = targetFormData.getData();
            JSONObject resultJson = thisFormData.getData();
            UpdateDataEntity updateDataEntity = new UpdateDataEntity();
            List<UpdateDataValueEntity> updateDataValueList = new ArrayList<>();
            updateDataEntity.setId(targetFormData.getDataId());
            updateDataEntity.setCorpid(targetFormData.getCorpid());
            updateDataEntity.setUpdateTime(now);
            updateDataEntity.setFormId(targetFormData.getFormId());
            if (isSubFormAndSubForm || isMainAndSubForm) {
                // 当前表单子表单字段赋值目标表单主字段
                EqualsResultPojo updateResultPojo = complexFormatHelp.equalsByCondition(targetFormData, targetFormData.getData(), operationDTO, null);
                List<JSONObject> resultList = updateResultPojo.getResultList();
                if (CollectionsUtil.isNotEmpty(resultList) && updateResultPojo.getUpdate()) {
                    resultJson = resultList.get(resultList.size()-1);
                }
            }
            Iterator<OperationsPojo> iterator = mainOperationList.iterator();
            while (iterator.hasNext()) {
                UpdateDataValueEntity updateDataValueEntity = new UpdateDataValueEntity();
                OperationsPojo operationsPojo = iterator.next();
                String targetAttr = operationsPojo.getAttr();
                List<String> targetAttrList = ExplainUtil.getAttrByPoint(targetAttr);
                if (targetAttrList.size() == 2) {
                    targetAttr = targetAttrList.get(0);
                }
                FieldAttrEntity targetField = targetExplainMap.get(targetAttr);
                if (Objects.isNull(targetField)) {
                    iterator.remove();
                    continue;
                }
                updateDataValueEntity.setKey(targetAttr);
                updateDataValueEntity.setEsKey(targetAttr);
                if (!ExplainUtil.isSubForm(targetField.getFieldType())) {
                    // 赋值给主字段
                    Map<String, Object> data = targetData.getInnerMap();
                    JSONObject targetSubFormData = null;
                    if (CollectionsUtil.isNotEmpty(subFormDataList)) {
                        targetSubFormData = subFormDataList.get(subFormDataList.size()-1).getData();
                    }
                    EqualsResultPojo equalsResultPojo = complexFormatHelp.newComplexFormat(operationDTO, operationsPojo, targetFormData, targetData,
                            businessRuleExplainPojo, businessRuleInfoPojo, resultJson, targetSubFormData);
                    if (equalsResultPojo.getUpdate()) {
                        String formula = equalsResultPojo.getFormula();
                        Object value = functionService.expressRun(formula, data);
                        value = PaasStringUtil.replaceQuotes(value);
                        value = complexFormatHelp.getTargetDataByFieldType(targetExplainMap, value, operationsPojo, targetField, ruleTargetPojo, corpid, businessRuleInfoPojo);
                        value = DataTypeUtil.setDataType(updateDataValueEntity, value, targetField.getFieldType());
                        updateDataValueEntity.setValue(value);
                        updateDataValueList.add(updateDataValueEntity);
                    }

                }
            }
            updateDataEntity.setData(updateDataValueList);
            updateDataList.add(updateDataEntity);
        }
        return updateDataList;
    }

}
